package com.yuchen.ishop.web.admin;

import com.yuchen.core.MyPager;
import com.yuchen.ishop.Gobals;
import com.yuchen.ishop.entity.<%= beanInfo.classname%>;
import com.yuchen.ishop.service.admin.<%= beanInfo.classname%>Service;
import com.yuchen.ishop.web.BaseAction;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping(value = "/admin/<%= beanInfo.alias%>")
public class <%= beanInfo.classname%>Action extends BaseAction {

    private final static Logger LOG = Logger.getLogger(<%= beanInfo.classname%>Action.class);

    @Autowired
    private <%= beanInfo.classname%>Service <%= beanInfo.alias%>Service;

    @RequestMapping(value = "/index")
    public String index(MyPager myPager,ModelMap modelMap,HttpServletRequest request)throws Exception {
        Map params =convertParameterMap(request.getParameterMap()) ;
        modelMap.addAttribute("pager",<%= beanInfo.alias%>Service.find(params,myPager));
        return "admin/<%= beanInfo.alias%>/index";
    }

    @RequestMapping(value = {"/add", "/edit"})
    public String add_edit(Long id,ModelMap modelMap)throws Exception {
        if(null == id) {
            return "admin/<%= beanInfo.alias%>/add";
        }
        else{
            modelMap.addAttribute("<%= beanInfo.alias%>",<%= beanInfo.alias%>Service.getById(id));
            return "admin/<%= beanInfo.alias%>/edit";
        }
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(<%= beanInfo.classname%> <%= beanInfo.alias%>,RedirectAttributes redirectAttributes)throws Exception {
        <%= beanInfo.alias%>Service.insert(<%= beanInfo.alias%>);
         redirectAttributes.addFlashAttribute(Gobals.__STATE__,true);
        return "redirect:/admin/<%= beanInfo.alias%>/index.do";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(<%= beanInfo.classname%> <%= beanInfo.alias%>,Long pageNo,RedirectAttributes redirectAttributes)throws Exception {
        <%= beanInfo.alias%>Service.update(<%= beanInfo.alias%>,true);
         redirectAttributes.addFlashAttribute(Gobals.__STATE__,true);
        if(null == pageNo || pageNo<1) pageNo = 1l;
        return "redirect:/admin/<%= beanInfo.alias%>/index.do?pageNo="+pageNo;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Long id,Long pageNo,RedirectAttributes redirectAttributes)throws Exception {
        <%= beanInfo.alias%>Service.delete(id);
         redirectAttributes.addFlashAttribute(Gobals.__STATE__,true);
        if(null == pageNo || pageNo<1) pageNo = 1l;
        return "redirect:/admin/<%= beanInfo.alias%>/index.do?pageNo="+pageNo;
    }

  

}
